import axios from '../../plugins/axios.js'

const state={
   list:[],
   shopcar:[]
}

let actions = {
	A_LIST_SETLIST:({commit,state},payload)=>{
		axios({
			url:'/api/caidan'
		}).then(res=>{
			commit('M_LIST_SETLIST',res.data.data)
		})
	},
	A_LIST_ADDSHOP({commit,state},payload){
		let bl=true
		state.shopcar.forEach((val,index)=>{
			if(val.name===payload.name){
				state.shopcar[index].num++
				bl=false
				commit('M_LIST_ADDSHOP',[...state.shopcar])
			}
		})
		if(bl){
			payload.num=1
			state.shopcar.push(payload)	
			commit('M_LIST_ADDSHOP',[...state.shopcar])
		}
	},
	A_LIST_JIAJIAN({commit,state},payload){
		state.shopcar.forEach((val,index)=>{
			if(val.name===payload.name){
				state.shopcar[index].num+=payload.num-0
			}
		})
		commit('M_LIST_ADDSHOP',[...state.shopcar])
	},
	A_LIST_REMOVEONE({commit,state},payload){
		state.shopcar.forEach((val,index)=>{
			if(val.id===payload){
				  state.shopcar.splice(index,1)
			}
		})
		commit('M_LIST_ADDSHOP',[...state.shopcar])
	},
	A_LIST_REMOVEALL({commit,state},payload){
		state.shopcar=[]
		commit('M_LIST_ADDSHOP',[...state.shopcar])
	}
}

let mutations = {
	M_LIST_SETLIST:(state,payload)=>{
		state.list=payload
	},
	M_LIST_ADDSHOP(state,payload){
		state.shopcar=payload
	}
}
let getters = {
	G_LIST_TESE:(state)=>{
		let arr=[]
		state.list.forEach((val,index)=>{
			if(val.pid==1){
				arr.push(val)
			}
		})
		return arr
	},
	G_LIST_SHAOKAO(state){
		let arr=[]
		state.list.forEach((val,index)=>{
			if(val.pid==2){
				arr.push(val)
			}
		})
		return arr
	},
	G_LIST_ZHUSHI(state){
		let arr=[]
		state.list.forEach((val,index)=>{
			if(val.pid==3){
				arr.push(val)
			}
		})
		return arr
	},
	G_LIST_YINPIN(state){
		let arr=[]
		state.list.forEach((val,index)=>{
			if(val.pid==4){
				arr.push(val)
			}
		})
		return arr
	},
	G_LIST_SHOPCAR(state){
		return state.shopcar
	}
}
export default{
  namespaced:true,//命名空间的模块。模块内部直接调用，外部需要命名空间(见组件)
  state,actions,mutations,getters
}